package com.cheetah.start.common.plugin.log;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.regex.Pattern;

@Slf4j
public class ConsoleSqlLog extends com.p6spy.engine.spy.appender.StdoutLogger {

    public ConsoleSqlLog() {
    }

    @Override
    public void logText(String text) {
        if (StrUtil.isNotBlank(text)) {
            String toLowerCase = text.toLowerCase();
            String insertPattern = "insert*into*values*";
            String updatePattern = "update*set*";
            String deletePattern = "delete*from*";
            boolean insert = Pattern.matches(insertPattern, toLowerCase);
            boolean update = Pattern.matches(updatePattern, toLowerCase);
            boolean delete = Pattern.matches(deletePattern, toLowerCase);
            if (insert || update || delete) {
                if (log.isDebugEnabled()) {
                    log.debug(text);
                } else {
                    log.info(text);
                }
            } else {
                log.debug(text);
            }
        }
    }
}
